home *** CD-ROM | disk | FTP | other *** search
/ Compendium Deluxe 1 / LSD Compendium Deluxe 1.iso / a / programming / assembly / ellipseasm.lha / circles.code / text0000.txt < prev   
Encoding:
Text File  |  1989-10-24  |  4.3 KB  |  135 lines

  1. >From mcdonald@uxe.cso.uiuc.edu Wed Jan  6 12:31:00 1988
  2. Path: leah!uwmcsd1!bbn!oberon!bloom-beacon!gatech!purdue!i.cc.purdue.edu!j.cc.purdue.edu!pur-ee!uiucdcs!uxc.cso.uiuc.edu!uxe.cso.uiuc.edu!mcdonald
  3. From: mcdonald@uxe.cso.uiuc.edu
  4. Newsgroups: comp.graphics
  5. Subject: Re: circles with non-unity aspect ratio
  6. Message-ID: <46900007@uxe.cso.uiuc.edu>
  7. Date: 6 Jan 88 17:31:00 GMT
  8. References: <807@hsi.UUCP>
  9. Lines: 120
  10. Nf-ID: #R:hsi.UUCP:807:uxe.cso.uiuc.edu:46900007:000:3894
  11. Nf-From: uxe.cso.uiuc.edu!mcdonald    Jan  6 11:31:00 1988
  12.  
  13.  
  14. >I am trying to draw "good" circles on an AT&T 3b1 (aka, UNIX PC).
  15. >The problem is the aspect ratio - it is nowhere near unity.
  16. >I have gone through McIlroy's paper ("Best Approximate Circles on
  17. >Integer Grids") and Foley & Van Dam.  I see two ways to draw the circle:
  18.  
  19. >(1) Utilize "user coordinates" (in this case a 4096 by 4096 square)
  20.        Bad idea.
  21.  
  22. >(2) Utilize "device coordinates" (430 by 288, in this case).
  23.         The correct way.
  24.  
  25. >Am I missing something simple that would make it easy to draw my
  26. >circles ??  Can anyone point me to another reference that adequately
  27. >covers the drawing of a circle on a screen with a non-unity aspect
  28. >ratio (F & VD mention a few, but before I go digging up the articles,
  29. >does anyone know if these are what I should be looking at) ??
  30.  
  31. It's not really easy to generate good looking circles on a pixel grid.
  32. After trying lots of different things, I have adopted the following
  33. ellipse drawer. It draws ellipses in pixel space; to get circles you
  34. feed it the proper height and width in pixels. This thine works really
  35. well for circles with a radius greater than 4. For smaller circles,
  36. it pays in terms of looking nice to draw each size as a special case.
  37.  
  38. The enclosed program uses the standard Bresnahan algorithm for the
  39. best approximation of a curve. It is optimized for speed.
  40.  
  41. There was a good reference to this an article in Dr. Dobbs Journal in 1987,
  42. but for circles only, not ellipses.
  43.  
  44. I hope that this can help you.
  45.  
  46. Doug McDonald
  47. Department of Chemistry
  48. University of Illinois
  49.  
  50.  
  51. /* Draw an ellipse with width irx and height iry                         */
  52. /* from a routine by Tim Hogan in Dr. Dobb's Journal May '85 p.40        */
  53. /* Improved by calculating increments incrementally, thus removing all   */
  54. /* multiplies from the loops. These multiplies were very bad since they  */
  55. /* were (long)*(long).                                                   */
  56. /* Written Sept. 7, 1987 by J.D. McDonald (public domain)                */
  57.  
  58. static long     alpha, beta, alpha2, alpha4, beta2, beta4, d;
  59. static long     ddx, ddy, alphadx, betady;
  60. static int      dy, dx;
  61.  
  62. extern void     e_start(int, int, int ,int);
  63. extern void     e_xd();    
  64. extern void     e_xdyu();
  65. extern void     e_yu(); 
  66.  
  67. ellipse(x, y, irx, iry, c)
  68.     int             x, y, irx, iry;
  69.     unsigned        c;
  70. {
  71.  
  72.     beta = (long) irx *(long) irx;
  73.     alpha = (long) iry *(long) iry;
  74.  
  75.     if (alpha == 0L)
  76.     alpha = 1L;
  77.     if (beta == 0L)
  78.     beta = 1L;
  79.  
  80.     dy = 0;
  81.     dx = irx;
  82.     alpha2 = alpha << 1;
  83.     alpha4 = alpha2 << 1;
  84.     beta2 = beta << 1;
  85.     beta4 = beta2 << 1;
  86.     alphadx = alpha * dx;
  87.     betady = 0;
  88.     ddx = alpha4 * (1 - dx);
  89.     ddy = beta2 * 3;
  90.  
  91.     d = alpha2 * ((long) (dx - 1) * dx) + alpha + beta2 * (1 - alpha);
  92.     e_start(x - dx, x + dx, y, c);
  93.           /* e_start draws left and rightmost pixels on vertical centerline */
  94.           /* e_yu draws a pixel in right top quadrant one up from previous  */
  95.           /* e_xd draws a pixel in right top quadrant one left from previous*/
  96.           /* e_xdyu draws a pixel in right top quadrant up and left from    */
  97.           /* previous. e_yu, e_xd, and e_xdyu also draw the corresponding   */
  98.           /* pixels in the other three quadrants.                           */
  99.           /* c is the color                                                 */
  100.     do {
  101.     if (d >= 0) {
  102.         d += ddx;
  103.         dx--;
  104.         alphadx -= alpha;
  105.         ddx += alpha4;
  106.         e_xdyu();
  107.     } else
  108.         e_yu();
  109.     d += ddy;
  110.     dy++;
  111.     betady += beta;
  112.     ddy += beta4;
  113.     } while (alphadx > betady);
  114.  
  115.     d = beta2 * ((long) dy * (dy + 1)) + alpha2 * ((long) dx * (dx - 2) + 1) 
  116.     + beta * (1 - alpha2);
  117.     ddx = alpha2 * (3 - (dx << 1));
  118.     ddy = beta4 * (1 + dy);
  119.  
  120.     do {
  121.     if (d <= 0) {
  122.         d += ddy;
  123.         ddy += beta4;
  124.         dy++;
  125.         e_xdyu();
  126.     } else
  127.         e_xd();
  128.     d += ddx;
  129.     ddx += alpha4;
  130.     dx--;
  131.     } while (dx > 0);
  132. }
  133.  
  134.  
  135.